<template>
	<div>
		<home-guess-you-like v-if="guessYouLikedata" :guessYouLike="guessYouLikedata" :guessYouLikeid="guessYouLikeid"></home-guess-you-like>
		<div v-if="isShowLoadMore"
               style="margin-top: -20px;color:#444444 ;height:2rem!important;  width: 100%;text-align: center;  line-height: 2rem;vertical-align: middle ; background:white;font-weight: 600"
               @click.self="changeFloat">
            <LoadMoreAnimate></LoadMoreAnimate>
            正在加载...
          </div>
	</div>
</template>
<script>
	import axios from "axios";
	import HomeGuessYouLike from "@/components/HomeGuessYouLike";
	import { getPageNameOrVirtualPageName } from "../../../utils/getPageName";
	import LoadMoreAnimate from '@/components/LoadMoreAnimate';
	export default {
		props: {
			guessYouLikeid: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				guessYouLikedata: [],
				storeCodedata: "",
				pageSize: 10,
				pageNum: 1,
				totalCount: 0,
				isShowLoadMore:false,
			}
		},
		methods: {
			/*queryfinalListNew() {
				let url = window.api.goods_host + "/resourceConfig/mobile/self_def";
				let options = {
					method: "get",
					url,
					params: {
						enterpriseCode: api.enterpriseCode,
						storeCode: this.storeCodedata,
						usePlatform: "wap"
					},
				}
				axios(options).then(data => {
					let arrey=[]
					if(data.data.data) {
						arrey = data.data.data[0]? data.data.data[0] : [];
						this.guessYouLikedata = arrey[`${this.guessYouLikeid}`]
					}
				})
			},*/
			queryfinalListNew() {
				if(this.pageNum==1 && this.guessYouLikedata.length>10){
					return 
				}
				this.storeCodedata = localStorage.getItem('storeCode')
				let url = window.api.goods_host + "/mallGoods/queryMallGoodsList";
				let options = {
					method: "post",
					url,
					data: {
						enterpriseCode: api.enterpriseCode,
						storeCode: this.storeCodedata,
						usePlatform: "wap",
						isCollectionSort: '2',
						isPage: true,
						pageNum: this.pageNum,
						length: this.pageSize,
					},
				}
				axios(options).then(data => {
					this.totalCount = data.data.data[0].totalCount
					var guessdata = data.data.data[0].results
					var arreyOld = this.guessYouLikedata.concat(guessdata)
					var arreyNew=[]
					for(var i=0;i<arreyOld.length;i++){
						if(arreyNew.indexOf(arreyOld[i])==-1){
							arreyNew.push(arreyOld[i])
						}
					}
					this.guessYouLikedata = arreyNew
					this.initCache()
					if(this.pageNum==2){
						this.isShowLoadMore = false;
					}
				})
			},
			loadMore() {
				if(this.pageNum == 2){
					return
				}
				if(this.totalCount > this.pageNum * this.pageSize && this.guessYouLikedata.length<11) {
					this.isShowLoadMore = true;
					this.pageNum = 2
					this.queryfinalListNew()
				}
			},
			inite() {
				PSEvent.listen("guessYouLikeUpdate", () => {
					this.queryfinalListNew()
				});
			},
			initCache() {
				sessionStorage.setItem("guessYouLikedata", JSON.stringify(this.guessYouLikedata));
				sessionStorage.setItem("guessYouLiketotalCount", JSON.stringify(this.totalCount))
			},
			loadData() {
				if(sessionStorage.getItem("guessYouLikedata")) {
					//console.log("loadData---读取本地缓存", );
					this.guessYouLikedata = JSON.parse(sessionStorage.getItem("guessYouLikedata"));
					this.totalCount = JSON.parse(sessionStorage.getItem("guessYouLiketotalCount"));
				} else {
					//console.log("本地没有缓存");
					this.queryfinalListNew()
				}
			}
		},
		/*watch: {
			guessYouLikeid() {
				this.queryfinalListNew()
				this.inite()
			}
		},*/
		components: {
			HomeGuessYouLike,
			LoadMoreAnimate,
		},
		computed: {
			pageName() {
				return getPageNameOrVirtualPageName();
			},
		},
		created() {
			this.inite()
			this.loadData()
		},
		mounted() {
			PSEvent.listen('home', ()=>{
				this.pageNum=1
				this.guessYouLikedata = []
				this.queryfinalListNew()
			})
			PSEvent.listen('scrollBottom',()=>{
				if(this.pageName=="home"){
					this.loadMore()
				}
			})
		},
	}
</script>
<style scoped>

</style>